package com.ydgk.jishuzhan.controller;


import com.ydgk.jishuzhan.beans.*;
import com.ydgk.jishuzhan.dao.UserMapper;
import com.ydgk.jishuzhan.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private TalkService talkService;

    @ResponseBody
    @RequestMapping("/doRegister")
    public AjaxResult doResister(User user){
        AjaxResult ajaxResult = new AjaxResult();

        try{
            userService.register(user);
            ajaxResult.setSuccess(true);
        } catch (Exception e){
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("toUser-home-for-me")
    public String toUserHomeForMe(Integer id,Model model){
        List<Blog> blogList=blogService.findBlogsById(id);
        model.addAttribute("blogs",blogList);
        List<Question> questions=questionService.findQuestionsById(id);
        List<Talk> talks=talkService.findTalksById(id);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "user-home-for-me";
    }


    @RequestMapping("toUser-home-for-other")
    public String toUserHomeForOther( Integer id,Model model){
        List<Blog> blogList=blogService.findBlogsById(id);
        model.addAttribute("blogs",blogList);
        List<Question> questions=questionService.findQuestionsById(id);
        List<Talk> talks=talkService.findTalksById(id);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        return "user-home-for-other";
    }



    @RequestMapping("toUser-info")
    public String toUserInfo(Integer id,Model model){
        UserInfo userInfo=userInfoService.findOne(id);
        System.out.println(userInfo);
        User user=userService.findUserById(id);
        model.addAttribute("user",user);
        model.addAttribute("userInfo",userInfo);
        return "user-info";
    }

    @ResponseBody
    @RequestMapping("/doEdit")
    public AjaxResult doEdit(User user){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            userService.updateUser(user);
            ajaxResult.setSuccess(true);
        }catch ( Exception e ) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }


    @ResponseBody
    @RequestMapping("/updatePassword")
    public AjaxResult updatePassword(User user){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            userService.updatePassword(user);
            ajaxResult.setSuccess(true);
        }catch ( Exception e ) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/guanzhu")
    public AjaxResult goodAdd(@Param("userId")Integer userId, @Param("friendId") Integer friendId){
        AjaxResult ajaxResult = new AjaxResult();
        int total=userService.findFriend(userId,friendId);
        if (total==0){
            try{
                userService.guanzhu(userId,friendId);
                ajaxResult.setSuccess(true);
            }catch (Exception e){
                ajaxResult.setSuccess(false);
                e.printStackTrace();
            }
        }else {
            ajaxResult.setSuccess(false);
        }

        return ajaxResult;
    }


    @RequestMapping(value = "/toSocial", method = RequestMethod.GET)
    public String toIndex(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                          @RequestParam(value= "userId" ,required = false ,defaultValue="1") Integer userId,
                          ModelMap modelMap) {

        Integer pageStart = (pageNo - 1) * pageSize;

        // 将请求参数封装成一个map
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageStart);
        map.put("size", pageSize);
        map.put("userId",userId);

        // 获取用户数据

        List<Integer> friendIds=userService.findFriends(map);

        List<User> users=new ArrayList<>();

        for (Integer i:friendIds){
            users.add(userService.findUserById(i));
        }

        // 将获取到的数据存入到模型中
        //modelMap.addAttribute("messages",messages);
        //将页面中需要的数据都传入到模型中   比如  pageNo   pageTotal  messages

        // 页码的总数   ===>  数据的条数    /   页面的大小   == > 页码的总数
        Integer pageTotal = 0;
        // 数据的条数
        Integer FriendTotal = userService.queryFriendTotal( map );

        if (FriendTotal % pageSize == 0) {
            pageTotal = FriendTotal / pageSize;
        } else {
            pageTotal = FriendTotal / pageSize + 1;
        }

        // 将数据封装到模型中
//        modelMap.addAttribute("messages",messages);
//        modelMap.addAttribute("pageNo",pageNo);
//        modelMap.addAttribute("pageTotal",pageTotal);

        // 构建一个对象，将需要的数据封装到对象中，传入页面时直接传入对象
        Page<User> page = new Page(users, pageNo, pageTotal);

        modelMap.addAttribute("page",page);

        return "social";
    }

}
